package com.example.myactspringboot;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Create 2023-10-18 21:19
 * @Description
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyActSpringBootApplication.class)
public class Subprocess {

    @Autowired
    private TaskService taskService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    IdentityService identityService;
    @Autowired
    HistoryService historyService;

    @Test
    public void initialize() throws Exception {
        //获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //完成流程部署操作，需要通过repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //完成部署  部署多个会使用最新版本
        Deployment deployment = repositoryService
                .createDeployment()
                .addClasspathResource(
                        "processes/My_Subprocess.bpmn20.xml")
                .deploy();
    }



@Test
    public void testSubProcess() {
        // prepare data packet
        Map<String, Object> variables = new HashMap<String, Object>();
        Map<String, Object> subVariables = new HashMap<String, Object>();
        variables.put("maxTransCount", 1000000);
//        variables.put("merchant", new Merchant("ICBC"));
        variables.put("protocol", "UM32");
        variables.put("repository", "10.10.38.99:/home/shirdrn/repository");
        variables.put("in", subVariables);
        variables.put("out", new HashMap<String, Object>());

        // start process instance
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("MySubprocess", variables);

        // enter subprocess
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).orderByTaskName().asc().list();
//        assertEquals(2, tasks.size());

        for(Task task : tasks) {
            taskService.complete(task.getId());
        }

        // leave subprocess
        Task collectTask = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
//        assertEquals("Collect message", collectTask.getName());

        Map<String, Object> taskVariables = new HashMap<String, Object>();
        taskVariables.put("att", "anything you need");
        taskService.setVariable(collectTask.getId(), "oper", "shirdrn");
        taskService.complete(collectTask.getId(), taskVariables);
    }

}
